var dbConfig = require("../util/dbconfig")
const send_email = require("../util/own.js");

function rand(min, max) {
	return Math.floor(Math.random() * (max - min)) + min
}
var validateemailCode = [];
let setcodetime = () => {
	setTimeout(function() {
		validateemailCode.pop();
	}, 1800000);
}
let sendCodeP = (email) => {
	for (var item of validateemailCode) {
		if (email == item.email) {
			return true;
		}
	}
	return false;
}

let findCodeAndPhone = (email, code) => {
	for (var item of validateemailCode) {
		if (email == item.email && code == item.code) {
			return 'login'
		}
	}
	return 'error';
}

let getLocalTime = (nS) => {
	var d = new Date(parseInt(nS)); //根据时间戳生成的时间对象
	var date = (d.getFullYear()) + "-" +
		(d.getMonth() + 1) + "-" +
		(d.getDate()) + " " +
		(d.getHours()) + ":" +
		(d.getMinutes()) + ":" +
		(d.getSeconds());
	return date;
}
//注册时发送邮箱验证码
var emailsendCode = (req, res) => {
	var email = req.body.email;
	if (sendCodeP(email)) {
		res.send({
			code: 200,
			msg: "返回成功",
			data: "邮箱发送过验证码，请稍后再试!",
		});
		return;
	}
	let randnumber = rand(100000, 999999);
	validateemailCode.push({
		email,
		code: randnumber
	});
	console.log(validateemailCode);
	setcodetime();
	let sql = `SELECT * from user where email = '${email}' `;
	var sqlArr = [email];
	var callBack = (err, data) => {
		if (err) {
			console.log('连接出错');
		} else {
			if (data.length) {
				res.send({
					code: 200,
					msg: "返回成功",
					data: "邮箱已注册",
				});
			} else {
				var mail = {
					// 发件人
					from: `"你爸爸给你发的验证码"<983500351@qq.com>`, //必须有<*****@qq.com>否则会报错Mail command failed: 502 Invalid paramenters
					// 主题
					subject: "验证码", //邮箱主题
					// 收件人
					to: email, //前台传过来的邮箱
					// 邮件内容，HTML格式
					text: "验证码为：" + randnumber, //发送验证码
				};
				// Initcode = code;
				send_email(mail);
				res.send({
					code: 200,
					msg: "返回成功",
					data: "发送成功",
				});
			}
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack);
}
//注册用户
let registered = (req, res) => {
	let {
		username,
		password,
		email,
		code,
		about
	} = req.body;
	//验证邮箱是否发送过验证码
	if (sendCodeP(email)) {
		//验证码和邮箱是否匹配
		let status = findCodeAndPhone(email, code);
		if (status == 'login') {
			//注册成功
			let create_time = new Date().getTime();
			var sql = "insert into user(username, password,email,create_time,status,role,about) values('" +
				username +
				"', '" +
				password + "','" + email + "','" + getLocalTime(create_time) + "','" + 1 + "','" + 1 + "','" +
				about + "')";
			var sqlArr = [username, password, email, create_time];
			var callBack = (err, data) => {
				if (err) {
					console.log('连接出错');
				} else {
					dbConfig.sqlConnect(`SELECT id from user where email = '${email}' `, [email], (err1,
						data1) => {
						res.send({
							'code': 200,
							'msg': '返回成功',
							'data': '注册成功',
							"userinfo": {
								"id": data1[0].id,
								"username": username,
								"create_time": create_time,
								"userpic": null,
								"email": email,
								"status": 1,
								"role": 1,
								"about": null
							}
						})
					})
				}
			}
			dbConfig.sqlConnect(sql, sqlArr, callBack)
		} else if (status == 'error') {
			res.send({
				'code': 200,
				'msg': "注册失败，验证码不存在"
			})
		}
	} else {
		res.send({
			'code': 400,
			'msg': "未发送验证码"
		})
	}
}

//登录用户
let signuser = (req, res) => {
	let {
		email,
		password
	} = req.body;
	let sql = `SELECT * from user where email = '${email}' `;
	var sqlArr = [email];
	var callBack = (err, data) => {
		if (err) {
			console.log('连接出错');
		} else {
			if (data.length) {
				if (data[0].password == password) {
					res.send({
						code: 200,
						msg: "返回成功",
						data: "用户登录成功",
						userinfo: {
							id: data[0].id,
							username: data[0].username,
							create_time: data[0].create_time,
							password: data[0].password,
							userpic: data[0].userpic,
							email: data[0].email,
							status: data[0].status,
							role: data[0].role,
							about: data[0].about
						}
					});
				} else {
					res.send({
						code: 200,
						msg: "返回成功",
						data: "用户密码错误",
					});
				}
			} else {
				res.send({
					code: 200,
					msg: "返回成功",
					data: "用户未注册",
				});
			}
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack);

}
//删除用户、文章、分类、项目、评论
let deleteid = (req, res) => {
	let {
		id,
		type
	} = req.query;
	if (type == '用户') {
		var sql = "delete from user where id=" + id;
	} else if (type == '文章') {
		var sql = "delete from article where id=" + id;
	} else if (type == '分类') {
		var sql = "delete from cate where id=" + id;
	} else if (type == '项目') {
		var sql = "delete from project where id=" + id;
	} else if (type == '评论') {
		var sql = "delete from discuss where id=" + id;
	}
	var sqlArr = [];
	var callBack = (err, data) => {
		if (err) {
			console.log('连接出错');
		} else {
			res.send({
				'code': 200,
				'msg': '返回成功',
				'data': '删除成功!'
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}
//修改用户密码
let modifyuserpass = (req, res) => {
	let {
		userid,
		password
	} = req.body;
	var sql = "update user set password='" + password + "' where id=" + userid;
	var sqlArr = [userid, password];
	var callBack = (err, data) => {
		if (err) {
			console.log('连接出错');
		} else {
			res.send({
				'code': 200,
				'msg': '返回成功',
				'data': '更新成功!'
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//根据用户id获取用户信息
let userinfo = (req, res) => {
	let {
		userid,
		pagesize,
		currentpage,
	} = req.query;
	if (userid != undefined && userid != '') {
		var sql = 'select id,username,userpic,create_time,email,status,role,password,about from user where id=' +
			userid;
	} else {
		var sql = 'select id,username,userpic,create_time,email,status,role,password,about from user';
	}
	var sqlArr = [];
	var callBack = (err, data) => {
		if (err) {
			console.log('连接出错');
		} else {
			let currentmin = pagesize * (Number(currentpage) - 1);
			let currentmax = pagesize * (Number(currentpage));
			res.send({
				'code': 200,
				'msg': '返回成功',
				'data': {
					"listpage": {
						"currentpage": Number(currentpage),
						"pagesize": Number(pagesize),
						"listcount": data.length
					},
					'List': data.slice(currentmin, currentmax)
				}
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//根据用户id获取该用户创建的文章列表
let userarticlelist = (req, res) => {
	let {
		userid,
		pagesize,
		currentpage,
		sort
	} = req.query;
	if (sort == 'desc') {
		if (userid != '' && userid != undefined) {
			var sql =
				'select a.id,a.name,a.pic,a.description,a.create_time,c.category,c.id as cate_id,b.username,a.content from article a,user b,cate c where a.user_id = b.id && a.user_id = "' +
				userid + '" && a.cate_id = c.id order by create_time ' +
				sort;
		} else {
			var sql =
				'select a.id,a.name,a.pic,a.description,a.create_time,c.category,c.id as cate_id,b.username,a.content from article a,user b,cate c where a.user_id = b.id && a.cate_id = c.id order by create_time ' +
				sort;
		}
	} else {
		if (userid != '' && userid != undefined) {
			var sql =
				'select a.id,a.name,a.pic,a.description,a.create_time,c.category,c.id as cate_id,b.username,a.content from article a,user b,cate c where a.user_id = b.id && a.user_id = "' +
				userid + '" && a.cate_id = c.id';
		} else {
			var sql =
				'select a.id,a.name,a.pic,a.description,a.create_time,c.category,c.id as cate_id,b.username,a.content from article a,user b,cate c where a.user_id = b.id && a.cate_id = c.id';
		}
	}
	var sqlArr = [userid];
	var callBack = (err, data) => {
		if (err) {
			console.log('连接出错');
		} else {
			let currentmin = pagesize * (Number(currentpage) - 1);
			let currentmax = pagesize * (Number(currentpage));
			res.send({
				'code': 200,
				'msg': '返回成功',
				'data': {
					"listpage": {
						"currentpage": Number(currentpage),
						"pagesize": Number(pagesize),
						"listcount": data.length
					},
					'List': data.slice(currentmin, currentmax)
				}
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//根据用户id获取该用户创建的分类列表
let usercatelist = (req, res) => {
	let {
		userid,
		pagesize,
		currentpage
	} = req.query;
	if (userid != '' && userid != undefined) {
		var sql =
			'select a.id,a.category,a.status,a.create_time,b.username from cate a,user b where a.user_id = b.id && user_id=' +
			userid;
	} else {
		var sql =
			'select a.id,a.category,a.status,a.create_time,b.username from cate a,user b where a.user_id = b.id';
	}

	var sqlArr = [];
	var callBack = (err, data) => {
		if (err) {
			console.log('连接出错');
		} else {
			let currentmin = pagesize * (Number(currentpage) - 1);
			let currentmax = pagesize * (Number(currentpage));
			if (pagesize != undefined) {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': {
						"listpage": {
							"currentpage": Number(currentpage),
							"pagesize": Number(pagesize),
							"listcount": data.length
						},
						'List': data.slice(currentmin, currentmax)
					}
				})
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': {
						'List': data
					}
				})
			}
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//根据用户id和文章id获取文章内容和文章评论
let userarticledis = (req, res) => {
	let {
		articleid
	} = req.query;
	var sql = `select if(T.type="t1",T.create_time1,
			(if(T.type="t2",T.create_time2,T.create_time3))) as create_time, 
					T.type, 
					T.userid, 
					T.article_id,
					T.username,
					T.userpic,
					T.id,
					T.dis_id,
					T.content from((select nc.id,nc.id as dis_id, nk.username ,nk.userpic,n.create_time as create_time1, nc.create_time as create_time2, "" as create_time3, "t2" as type, nc.userid, nc.article_id, 
						concat("    发表评论:",nc.con_dis) as content from discuss nc left join article n on nc.article_id=n.id left join user nk on nk.id=nc.userid
						where nc.article_id = ${articleid}
						order by n.create_time desc, nc.create_time asc) 
						union (select nr.id,nr.dis_id as dis_id ,nk.username ,nk.userpic,n.create_time as create_time1, nc.create_time as create_time2, nr.create_time as create_time3, "t3" as type, nr.userid, nc.article_id, 
						concat("        回复",IFNULL(nk2.username,""),"：",nr.reply_con) as content from reply nr 
						left join reply nr1 on nr1.id = nr.reply_id 
						left join discuss nc on nr.dis_id=nc.id 
						left join article n on nc.article_id=n.id
						left join user nk on nk.id = nr.userid
						left join user nk2 on nr1.userid = nk2.id  
						where nc.article_id = ${articleid}
						order by n.create_time desc, nc.create_time asc, nr.create_time asc
						)) as T 
						order by create_time1 desc, create_time2 asc, create_time3 asc
					 
					 `
	var sqlArr = [];
	var callBack = (err, data) => {
		if (err) {
			console.log('连接出错');
		} else {
			dbConfig.sqlConnect(
				'select a.name,a.content,b.username,a.create_time from article a,user b where a.id = "' +
				articleid +
				'" && a.user_id = b.id', [], (err1, data1) => {
					data.map((val, inx) => {
						val.hobbies = eval(val.hobbies);
					});
					res.send({
						'code': 200,
						'msg': '返回成功',
						'data': {
							'article': data1[0],
							'dis': data
						}
					})
				})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//根据用户id获取该用户创建的项目
let userproject = (req, res) => {
	let {
		userid,
		pagesize,
		currentpage
	} = req.query;
	if (userid != undefined && userid != '') {
		var sql =
			'select a.id,a.projectname,a.create_time,a.projecturl,b.username from project a,user b where a.user_id = b.id && user_id = ' +
			userid;
	} else {
		var sql =
			'select a.id,a.projectname,a.create_time,a.projecturl,b.username from project a,user b where a.user_id = b.id';
	}
	var sqlArr = [userid];
	var callBack = (err, data) => {
		if (err) {
			console.log('连接出错');
		} else {
			if (pagesize != undefined) {
				let currentmin = pagesize * (Number(currentpage) - 1);
				let currentmax = pagesize * (Number(currentpage));
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': {
						"listpage": {

							"currentpage": Number(currentpage),
							"pagesize": Number(pagesize),
							"listcount": data.length
						},
						'List': data.slice(currentmin, currentmax)
					}
				})
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': {
						'List': data
					}
				})
			}

		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//根据用户id获取该用户创建的评论
let userdis = (req, res) => {
	let {
		userid,
		pagesize,
		currentpage,
		sort
	} = req.query;
	if (sort == 'desc') {
		if (userid != undefined && userid != '') {
			var sql =
				'select a.id,b.username,c.name,a.con_dis,a.create_time,a.article_id from discuss a,user b, article c where a.userid = ' +
				userid + '= b.id && a.article_id = c.id order by a.create_time ' + sort;
		} else {
			var sql =
				'select a.id,b.username,c.name,a.con_dis,a.create_time,a.article_id from discuss a,user b, article c where a.userid = b.id && a.article_id = c.id order by a.create_time ' +
				sort;
		}
	} else {
		if (userid != undefined && userid != '') {
			var sql =
				'select a.id,b.username,c.name,a.con_dis,a.create_time,a.article_id from discuss a,user b, article c where a.userid = ' +
				userid + '= b.id && a.article_id = c.id';
		} else {
			var sql =
				'select a.id,b.username,c.name,a.con_dis,a.create_time,a.article_id from discuss a,user b, article c where a.userid = b.id && a.article_id = c.id';
		}
	}

	var sqlArr = [userid];
	var callBack = (err, data) => {
		if (err) {
			console.log('连接出错');
		} else {
			let currentmin = pagesize * (Number(currentpage) - 1);
			let currentmax = pagesize * (Number(currentpage));
			res.send({
				'code': 200,
				'msg': '返回成功',
				'data': {
					"listpage": {

						"currentpage": Number(currentpage),
						"pagesize": Number(pagesize),
						"listcount": data.length
					},
					'List': data.slice(currentmin, currentmax)
				}
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//根据分类id获取分类下的文章列表
let catearticle = (req, res) => {
	let {
		userid,
		cateid,
		pagesize,
		currentpage
	} = req.query;
	if(cateid != undefined && cateid != ""){
		var sql =
			'select id,name,pic,description,create_time from article where cate_id = ' + cateid + '&& user_id = '+ userid;
	}else {
		var sql =
			'select id,name,pic,description,create_time from article where user_id = '+ userid;
	}
	var sqlArr = [cateid,userid];
	var callBack = (err, data) => {
		if (err) {
			console.log('连接出错');
		} else {
			if (pagesize != undefined) {
				let currentmin = pagesize * (Number(currentpage) - 1);
				let currentmax = pagesize * (Number(currentpage));
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': {
						"listpage": {

							"currentpage": Number(currentpage),
							"pagesize": Number(pagesize),
							"listcount": data.length
						},
						'List': data.slice(currentmin, currentmax)
					}
				})
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': {
						'List': data
					}
				})
			}

		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}


//根据用户id获取用户关于我信息
let getabout = (req, res) => {
	let {
		userid
	} = req.query;
	var sql =
		'select about from user where id = ' + userid;
	var sqlArr = [];
	var callBack = (err, data) => {
		if (err) {
			console.log('连接出错');
		} else {
			res.send({
				'code': 200,
				'msg': '返回成功',
				'data': data
			})
		}
	}
	dbConfig.sqlConnect(sql, sqlArr, callBack)
}
//新建编辑评论
let addoreditdiscuss = (req, res) => {
	let {
		id,
		userid,
		con_dis,
		article_id
	} = req.body;
	if (id != undefined && id != '') {
		console.log(
			id,
			userid,
			con_dis,
			article_id
		)
		var sql = "UPDATE discuss set con_dis='" + con_dis + "',article_id='" + article_id +
			"' WHERE id=" + id;
		var sqlArr = [];
		var callBack = (err, data) => {
			if (err) {
				console.log('连接出错');
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': '编辑成功'
				})
			}
		}
	} else {
		let create_time = new Date().getTime();
		var sql = "insert into discuss(userid, con_dis,article_id,create_time) values('" + userid +
			"', '" +
			con_dis + "','" + article_id + "','" + getLocalTime(create_time) + "')";
		var sqlArr = [];
		var callBack = (err, data) => {
			if (err) {
				console.log('连接出错');
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': '新建成功'
				})
			}
		}
	}

	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//新建编辑评论回复
let addoreditreply = (req, res) => {
	let {
		id,
		userid,
		reply_con,
		article_id,
		reply_id,
		dis_id
	} = req.body;
	if (id != undefined && id != '') {
		var sql = "UPDATE reply set reply_con='" + reply_con + "',article_id='" + article_id + "',reply_id='" +
			reply_id + "',dis_id='" + dis_id + "',userid='" + userid +
			"' WHERE id=" + id;
		var sqlArr = [];
		var callBack = (err, data) => {
			if (err) {
				console.log('连接出错');
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': '编辑成功'
				})
			}
		}
	} else {
		let create_time = new Date().getTime();
		if(reply_id == ''){
			var sql = "insert into reply(userid, reply_con,article_id,create_time,dis_id) values('" + userid + "', '" +
				reply_con + "','" + article_id + "','" + getLocalTime(create_time) + "','" + dis_id + "')";
		}else {
			var sql = "insert into reply(userid, reply_con,article_id,create_time,dis_id,reply_id) values('" + userid + "', '" +
				reply_con + "','" + article_id + "','" + getLocalTime(create_time) + "','" + dis_id + "','" + reply_id + "')";
		}
		var sqlArr = [];
		var callBack = (err, data) => {
			if (err) {
				console.log('连接出错');
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': '新建成功'
				})
			}
		}
	}

	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//新建编辑项目
let addoreditproject = (req, res) => {
	let {
		id,
		userid,
		projectname,
		projecturl
	} = req.body;
	if (id != undefined && id != '') {
		var sql = "UPDATE project set projectname='" + projectname + "',projecturl='" +
			projecturl +
			"' WHERE id=" + id;
		var sqlArr = [];
		var callBack = (err, data) => {
			if (err) {
				console.log('连接出错');
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': '编辑成功'
				})
			}
		}
	} else {
		let create_time = new Date().getTime();
		var sql = "insert into project(user_id, projectname,projecturl,create_time) values('" + userid +
			"', '" +
			projectname + "','" + projecturl + "','" + getLocalTime(create_time) + "')";
		var sqlArr = [userid, projectname, projecturl];
		var callBack = (err, data) => {
			if (err) {
				console.log('连接出错');
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': '新建成功'
				})
			}
		}
	}

	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//新建编辑分类
let addoreditcate = (req, res) => {
	let {
		id,
		userid,
		category,
		status
	} = req.body;
	if (id != undefined && id != '') {
		var sql = "UPDATE cate set category='" + category + "',status='" +
			status +
			"' WHERE id=" + id;
		var sqlArr = [];
		var callBack = (err, data) => {
			if (err) {
				console.log('连接出错');
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': '编辑成功'
				})
			}
		}
	} else {
		let create_time = new Date().getTime();
		var sql = "insert into cate(user_id, category,status,create_time) values('" + userid +
			"', '" +
			category + "','" + status + "','" + getLocalTime(create_time) + "')";
		var sqlArr = [];
		var callBack = (err, data) => {
			if (err) {
				console.log('连接出错');
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': '新建成功'
				})
			}
		}
	}

	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//新建编辑文章
let addoreditarticle = (req, res) => {
	let {
		id,
		userid,
		name,
		description,
		pic,
		content,
		cate_id
	} = req.body;
	console.log(
		id,
		userid,
		name,
		description,
		pic,
		content,
		cate_id
	);
	if (id != undefined && id != '') {
		var sql = "UPDATE article set name='" + name + "',description='" + description +
			"',pic='" + pic + "',content='" + content +
			"',cate_id='" + cate_id +
			"' WHERE id=" + id;
		var sqlArr = [];
		var callBack = (err, data) => {
			if (err) {
				console.log('连接出错');
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': '编辑成功'
				})
			}
		}
	} else {
		let create_time = new Date().getTime();
		var sql =
			"insert into article(user_id,name,description,create_time,pic,content,cate_id) values('" +
			userid +
			"', '" +
			name + "','" + description + "','" + getLocalTime(create_time) + "','" + pic + "','" + content + "','" +
			cate_id + "')";
		var sqlArr = [];
		console.log(sql);
		var callBack = (err, data) => {
			if (err) {
				console.log('连接出错');
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': '新建成功'
				})
			}
		}
	}

	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//新建编辑用户
let addoredituser = (req, res) => {
	let {
		id,
		username,
		userpic,
		password,
		email,
		status,
		role,
		about
	} = req.body;
	if (id != undefined && id != '') {
		var sql = "update user set username='" + username + "',userpic='" +
			userpic +
			"',password='" + password + "',email='" + email + "',status='" + status + "',role='" + role +
			"',about='" +
			about +
			"' where id=" + id;
		var sqlArr = [];
		var callBack = (err, data) => {
			if (err) {
				console.log('连接出错');
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': '编辑成功'
				})
			}
		}
	} else {
		let create_time = new Date().getTime();
		var sql =
			"insert into user( username,userpic,create_time,password,email,status,role,about) values('" +
			username + "','" + userpic + "','" + getLocalTime(create_time) + "','" + password + "','" + email +
			"','" +
			status + "','" + role + "','" + about + "')";
		var sqlArr = [];
		var callBack = (err, data) => {
			if (err) {
				console.log('连接出错');
			} else {
				res.send({
					'code': 200,
					'msg': '返回成功',
					'data': '新建成功'
				})
			}
		}
	}

	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//管理员获取所有文章、评论、用户数量。
let adminwpynum = (req, res) => {
	var sql = 'select * from article';
	var sqlArr = [];
	var callBack = (err, data) => {
		if (err) {
			console.log('连接出错');
		} else {
			dbConfig.sqlConnect('select * from discuss', [], (err1, data1) => {
				dbConfig.sqlConnect('select * from user', [], (err2, data2) => {
					res.send({
						'code': 200,
						'msg': '返回成功',
						'data': {
							'article': data.length,
							'discuss': data1.length,
							'user': data2.length
						}
					})
				})
			})

		}
	}


	dbConfig.sqlConnect(sql, sqlArr, callBack)
}

//上传图片。
let addUpload = (req, res) => {
	//接收普通文本参数
	let file = req.file;
	res.send({
		'code': 200,
		'msg': '上传成功',
		'data': "http://localhost:3000/static/uploads/" + file.filename
	})
}

module.exports = {
	emailsendCode, //发送邮箱验证码
	registered, //注册用户
	signuser, //登录用户
	deleteid, //删除用户、文章、分类、项目、评论
	modifyuserpass, //修改用户密码
	userinfo, //获取用户信息
	userarticlelist, //根据用户id获取该用户创建的文章列表
	usercatelist, //根据用户id获取该用户创建的分类列表
	userarticledis, //根据用户id和文章id获取文章内容和文章评论
	userproject, //根据用户id获取该用户创建的项目
	userdis, //根据用户id获取该用户创建的评论
	catearticle, //根据分类id获取分类下的文章列表
	getabout,//根据用户id获取用户的关于我信息
	addoreditdiscuss, //新建编辑评论
	addoreditreply, //新建编辑评论回复
	addoreditproject, //新建编辑项目
	addoreditcate, //新建编辑分类
	addoreditarticle, //新建编辑文章
	addoredituser, //新建编辑用户
	adminwpynum, //管理员获取所有文章、评论、用户数量。
	addUpload, //上传图片。
}
